<template>
<el-card class="box-card">
  <div slot="header" class="clearfix">
    <span>登录</span>
  </div>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="手机号" prop="phone">
    <el-input v-model.number="ruleForm.phone"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>

  
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</el-card>
  
</template>

<script>
import {login} from '../service'
import {mapMutations} from 'vuex'
export default {
data() {
    
      return {
        ruleForm: {
          pass: '',
          phone: ''
        },
        rules: {
          pass: [
            { required:true,message:'密码不能为空', trigger: 'blur' }
          ],
          
          phone: [
              { required:true,message:'手机号不能为空'},
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let response = await login(this.ruleForm.phone,this.ruleForm.pass)
            if(response.status == 1){
                this.$router.push('/manage');
            }else{
                alert('手机号或密码错误');
            }
            
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      ...mapMutations(["setLoginUser"])
    }
}
</script>

<style>
.box-card{
    width:500px;
    margin:auto;
}
</style>